package com.linkwechat.common.utils;/**
 * @Author 王君平
 * @Date 2024/6/11 10:07
 * @Description
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Author 王君平
 * @Date 2024/6/11 10:07
 * @Description
 */
public class CollectionUtil {

    /**
     * list集合中的数据,按照指定个数为一组
     *
     */
    public static <T> List<List<T>> groupList(List<T> list, int count){
        if(CollectionUtil.isEmpty(list)){
            return Collections.emptyList();
        }
        int listSize = list.size();
        int toIndex = count;
        List<List<T>> groupList = new ArrayList<>() ;    //用List存起来新的分组后数据
        for(int i = 0;i<list.size();i += count){
            if(i + count > listSize ){        //作用为toIndex最后没有count条数据则剩余几条newList中就装几条
                toIndex = listSize - i;
            }
            groupList.add(list.subList(i,i + toIndex));
        }
        return groupList;
    }

    public static boolean isEmpty(List list){
        return list == null || list.size() == 0;
    }

    public static boolean isNotEmpty(List list){
        return list != null && list.size() > 0;
    }

}
